//
//  SCNBorrowFootView.swift
//  ShoucainuSwift
//
//  Created by 王家旭 on 2017/3/27.
//  Copyright © 2017年 Riluee. All rights reserved.
//

import UIKit

class SCNBorrowFootView: UIView {
    
    typealias action = (Int) -> Void
    
    var footActionBlock: action?
    

    var contenView: UICollectionView!
    
    let dataArr = [["img": "icon_yezhu", "title": "业主贷"], ["img": "icon_chezhu", "title": "车主贷"], ["img": "icon_shouxian", "title": "寿险贷"], ["img": "icon_youfang", "title": "优房贷"], ["img": "icon_zhaiE", "title": "宅E贷"]]
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = SCNConstants.colorWithHexString("EEEEEE")
        
        configurationUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func configurationUI() {
        
        //1.
        let lineView = UIView(frame: CGRect(x: CGFloat(5), y: CGFloat(15), width: CGFloat(5), height: CGFloat(20)))
        lineView.backgroundColor = SCNConstants.colorWithHexString("#0092D6")
        addSubview(lineView)
        let titleLabel = UILabel(frame: CGRect(x: 15, y: 16, width: 150, height: 20))
        titleLabel.text = "贷款类型说明"
        titleLabel.font = UIFont.systemFont(ofSize: 19.0)
        addSubview(titleLabel)
        
        //2.
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.minimumInteritemSpacing = 0
        //cell间距
        flowLayout.minimumLineSpacing = 0
        //行间距
        flowLayout.itemSize = CGSize(width: SCNConstants.SCREENWIDTH/5, height: 100)
        contenView = UICollectionView(frame: CGRect(x: 0, y:40, width: SCNConstants.SCREENWIDTH, height: 100), collectionViewLayout: flowLayout)
        contenView.delegate = self
        contenView.dataSource = self
        contenView.backgroundColor = backgroundColor
        contenView.isScrollEnabled = false
        addSubview(contenView)
        contenView.register(SCNBorrowFootItem.classForCoder(), forCellWithReuseIdentifier: NSStringFromClass(SCNBorrowFootItem.classForCoder()))

    }
}

extension SCNBorrowFootView: UICollectionViewDelegate, UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataArr.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let item = collectionView.dequeueReusableCell(withReuseIdentifier: NSStringFromClass(SCNBorrowFootItem.classForCoder()), for: indexPath) as! SCNBorrowFootItem
        let obj = dataArr[indexPath.item]
        
        item.imgView.image = UIImage(named: obj["img"]!)
        item.detailsLabel.text = obj["title"]
        
        return item
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        defer {
            collectionView.deselectItem(at: indexPath, animated: true)
        }
        footActionBlock?(indexPath.item)
    }
}

class SCNBorrowFootItem: UICollectionViewCell {
    
    lazy var imgView: UIImageView = {
        
        let imgView = UIImageView(frame: CGRect(x: 10, y: 10, width: self.frame.size.width - 20, height: self.frame.size.width - 20))
        
        self.addSubview(imgView)
        
        return imgView
        
    }()
    
    
    
    
    lazy var detailsLabel: UILabel = {
        
        let details = UILabel(frame: CGRect(x: 0, y: self.frame.size.width - 5, width: self.frame.size.width, height: 20))
        
        details.textColor = SCNConstants.colorFromHexValue(0x3A3A3A, alpha: 1)
        
        details.font = UIFont.systemFont(ofSize: 16.0)
        
        details.textAlignment = .center
        
        self.addSubview(details)
        
        return details
        
    }()
}
